<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <base href="<%=basePath%>"/>
    <title>茶叶进销存系统 - 登录注册</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: url('https://images.unsplash.com/photo-1536514072410-5019a3c69182?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center fixed;
            background-size: cover;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
        }

        .auth-container {
            background: white; border-radius: 15px; box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden; width: 500px; max-width: 90vw;
        }
        .auth-header {
            background: linear-gradient(135deg, #8B9E6B, #7A8D5D); color: white;
            padding: 30px; text-align: center;
        }
        .auth-header h1 { font-size: 24px; margin-bottom: 10px; }
        .auth-header p { opacity: 0.9; font-size: 14px; }
        .auth-tabs { display: flex; background: #f8f9fa; }
        .auth-tab {
            flex: 1; padding: 15px; text-align: center; cursor: pointer;
            transition: all 0.3s; border-bottom: 3px solid transparent;
        }
        .auth-tab:hover, .auth-tab.active {
            background: white; border-bottom-color: #8B9E6B; color: #8B9E6B;
        }
        .auth-content { padding: 40px 30px; }
        .auth-form { display: none; }
        .auth-form.active { display: block; }
        .form-group { margin-bottom: 25px; }
        .form-group label { display: block; margin-bottom: 8px; color: #333; font-weight: 500; }
        .form-control {
            width: 100%; padding: 12px 15px; border: 2px solid #e1e5e9; border-radius: 8px;
            font-size: 14px; transition: all 0.3s; background-color: #f8f9fa;
        }
        .form-control:focus {
            outline: none; border-color: #8B9E6B; background-color: white;
            box-shadow: 0 0 0 3px rgba(139, 158, 107, 0.1);
        }
        .login-type {
            display: flex; margin-bottom: 25px; background: #f8f9fa;
            border-radius: 8px; padding: 5px;
        }
        .login-type input[type="radio"] { display: none; }
        .login-type label {
            flex: 1; text-align: center; padding: 10px; cursor: pointer;
            border-radius: 6px; transition: all 0.3s; font-weight: 500;
        }
        .login-type input[type="radio"]:checked + label { background: #8B9E6B; color: white; }
        .form-row { display: flex; gap: 15px; }
        .form-col { flex: 1; }
        .btn-auth {
            width: 100%; padding: 12px; background: linear-gradient(135deg, #8B9E6B, #7A8D5D);
            color: white; border: none; border-radius: 8px; font-size: 16px;
            font-weight: 600; cursor: pointer; transition: all 0.3s;
        }
        .btn-auth:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(139, 158, 107, 0.4); }
        .error-message {
            background: #fee; color: #c33; padding: 12px; border-radius: 8px;
            margin-bottom: 20px; border-left: 4px solid #c33;
        }
        .success-message {
            background: #e8f5e8; color: #2d5016; padding: 12px; border-radius: 8px;
            margin-bottom: 20px; border-left: 4px solid #8B9E6B;
        }
        .tea-icon { color: #8B9E6B; margin-right: 8px; }
        .form-help { font-size: 12px; color: #666; margin-top: 5px; }
        .demo-info {
            background: #e8f5e8; padding: 15px; border-radius: 8px;
            margin-bottom: 20px; font-size: 13px; color: #2d5016;
        }
        .demo-info h4 { margin-bottom: 8px; color: #1a3009; }
        .agreement {
            display: flex; align-items: center; gap: 10px;
            margin-bottom: 25px; font-size: 14px; color: #666;
        }
        .agreement input[type="checkbox"] { width: auto; }
        .agreement a { color: #8B9E6B; text-decoration: none; }
        .agreement a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-header">
            <h1><i class="fas fa-leaf"></i> 茶叶进销存系统</h1>
            <p>Tea Inventory Management System</p>
        </div>
        
        <div class="auth-tabs">
            <div class="auth-tab active" onclick="switchTab('login')">
                <i class="fas fa-sign-in-alt"></i> 登录
            </div>
            <div class="auth-tab" onclick="switchTab('register')">
                <i class="fas fa-user-plus"></i> 注册
            </div>
        </div>
        
        <div class="auth-content">
            <c:if test="${not empty error}">
                <div class="error-message"><i class="fas fa-exclamation-circle"></i> ${error}</div>
            </c:if>
            <c:if test="${not empty success}">
                <div class="success-message"><i class="fas fa-check-circle"></i> ${success}</div>
            </c:if>
            
            <!-- 登录表单 -->
            <div class="auth-form active" id="loginForm">
<%--                <div class="demo-info">--%>
<%--                    <h4><i class="fas fa-info-circle"></i> 演示账号</h4>--%>
<%--                    <p><strong>管理员：</strong> admin / admin</p>--%>
<%--                    <p><strong>客户：</strong> 任意手机号 / 123456</p>--%>
<%--                </div>--%>
                
                <form action="LoginServlet" method="post">
                    <div class="login-type">
                        <input type="radio" id="admin" name="userType" value="admin" checked>
                        <label for="admin"><i class="fas fa-user-cog tea-icon"></i>管理员</label>
                        <input type="radio" id="customer" name="userType" value="customer">
                        <label for="customer"><i class="fas fa-user tea-icon"></i>客户</label>
                    </div>
                    
                    <div class="form-group">
                        <label for="username">
                            <i class="fas fa-user tea-icon"></i><span id="usernameLabel">用户名</span>
                        </label>
                        <input type="text" id="username" name="username" class="form-control" 
                               placeholder="请输入用户名" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="password"><i class="fas fa-lock tea-icon"></i>密码</label>
                        <input type="password" id="password" name="password" class="form-control" 
                               placeholder="请输入密码" required>
                    </div>
                    
                    <button type="submit" class="btn-auth">
                        <i class="fas fa-sign-in-alt"></i> 登录
                    </button>
                </form>
            </div>
            
            <!-- 注册表单 -->
            <div class="auth-form" id="registerForm">
                <form action="RegisterServlet" method="post" id="regForm">
                    <div class="form-group">
                        <label for="customerName"><i class="fas fa-user tea-icon"></i>姓名</label>
                        <input type="text" id="customerName" name="customerName" class="form-control" 
                               placeholder="请输入您的姓名" required maxlength="50">
                    </div>
                    
                    <div class="form-group">
                        <label for="phone"><i class="fas fa-phone tea-icon"></i>手机号</label>
                        <input type="tel" id="phone" name="phone" class="form-control" 
                               placeholder="请输入手机号" required maxlength="11">
                        <div class="form-help">手机号将作为您的登录账号</div>
                    </div>
                    
                    <div class="form-group">
                        <label for="email"><i class="fas fa-envelope tea-icon"></i>邮箱地址（可选）</label>
                        <input type="email" id="email" name="email" class="form-control" 
                               placeholder="请输入邮箱地址" maxlength="100">
                    </div>
                    
                    <div class="form-row">
                        <div class="form-col">
                            <div class="form-group">
                                <label for="regPassword"><i class="fas fa-lock tea-icon"></i>密码</label>
                                <input type="password" id="regPassword" name="password" class="form-control" 
                                       placeholder="请输入密码" required minlength="6" maxlength="20">
                                <div class="form-help">密码长度6-20位</div>
                            </div>
                        </div>
                        <div class="form-col">
                            <div class="form-group">
                                <label for="confirmPassword"><i class="fas fa-key tea-icon"></i>确认密码</label>
                                <input type="password" id="confirmPassword" name="confirmPassword" class="form-control" 
                                       placeholder="请再次输入密码" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="gender"><i class="fas fa-venus-mars tea-icon"></i>性别</label>
                        <select id="gender" name="gender" class="form-control">
                            <option value="">请选择性别</option>
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                    </div>
                    
                    <div class="agreement">
                        <input type="checkbox" id="agreement" required>
                        <label for="agreement">
                            我已阅读并同意 <a href="#" onclick="showAgreement()">《用户协议》</a> 和 <a href="#" onclick="showPrivacy()">《隐私政策》</a>
                        </label>
                    </div>
                    
                    <button type="submit" class="btn-auth">
                        <i class="fas fa-user-plus"></i> 立即注册
                    </button>
                </form>
            </div>
        </div>
    </div>
    
    <script>
        function switchTab(tabName) {
            document.querySelectorAll('.auth-tab').forEach(tab => tab.classList.remove('active'));
            event.target.classList.add('active');
            document.querySelectorAll('.auth-form').forEach(form => form.classList.remove('active'));
            document.getElementById(tabName === 'login' ? 'loginForm' : 'registerForm').classList.add('active');
        }
        
        document.querySelectorAll('input[name="userType"]').forEach(radio => {
            radio.addEventListener('change', function() {
                const usernameLabel = document.getElementById('usernameLabel');
                const usernameInput = document.getElementById('username');
                if (this.value === 'admin') {
                    usernameLabel.textContent = '用户名';
                    usernameInput.placeholder = '请输入用户名';
                } else {
                    usernameLabel.textContent = '手机号';
                    usernameInput.placeholder = '请输入手机号';
                }
            });
        });
        
        document.querySelector('#loginForm form').addEventListener('submit', function(e) {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const userType = document.querySelector('input[name="userType"]:checked').value;
            
            if (!username || !password) {
                alert('用户名和密码不能为空');
                e.preventDefault();
                return;
            }
            
            if (userType === 'customer') {
                const phonePattern = /^1[3-9]\d{9}$/;
                if (!phonePattern.test(username)) {
                    alert('请输入正确的手机号格式');
                    e.preventDefault();
                    return;
                }
            }
        });
        
        document.getElementById('regForm').addEventListener('submit', function(e) {
            const customerName = document.getElementById('customerName').value.trim();
            const phone = document.getElementById('phone').value.trim();
            const password = document.getElementById('regPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const email = document.getElementById('email').value.trim();
            const agreement = document.getElementById('agreement').checked;
            
            if (!customerName || !phone) {
                alert('姓名和手机号不能为空');
                e.preventDefault();
                return;
            }
            
            const phonePattern = /^1[3-9]\d{9}$/;
            if (!phonePattern.test(phone)) {
                alert('请输入正确的手机号格式');
                e.preventDefault();
                return;
            }
            
            if (password.length < 6) {
                alert('密码长度不能少于6位');
                e.preventDefault();
                return;
            }
            
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                e.preventDefault();
                return;
            }
            
            if (email) {
                const emailPattern = /^[A-Za-z0-9+_.-]+@([A-Za-z0-9.-]+\.[A-Za-z]{2,})$/;
                if (!emailPattern.test(email)) {
                    alert('请输入正确的邮箱格式');
                    e.preventDefault();
                    return;
                }
            }
            
            if (!agreement) {
                alert('请先阅读并同意用户协议和隐私政策');
                e.preventDefault();
                return;
            }
        });
        
        function showAgreement() { alert('用户协议内容（此处应显示完整的用户协议）'); }
        function showPrivacy() { alert('隐私政策内容（此处应显示完整的隐私政策）'); }
    </script>
</body>
</html>
